package com.lst.projectlib.util;

import android.text.TextUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class StringUtil {

	public static String extract(String p, String str) {
		String[] args = str.split("&");
		for (int i = 0; i < args.length; i++) {
			if (!args[i].equals("") && args != null) {
				System.out.println("---");
				if (args[i].subSequence(0, args[i].indexOf("=")).equals(p)) {
					return args[i].substring(args[i].indexOf("=") + 1,
							args[i].length());
				}

			}
		}
		return null;
	}

	public static boolean isStrInString(String bigStr, String smallStr) {
		if (smallStr == null || "".equals(smallStr)) {
			return true;
		}
		
		if (bigStr == null || "".equals(bigStr)) {
			return false;
		}
		if (bigStr.toUpperCase().indexOf(smallStr.toUpperCase()) > -1) {
			return true;
		} else {
			return false;
		}
	}
	
	public static boolean isStrInStringStart(String bigStr, String smallStr) {
		if (smallStr == null || "".equals(smallStr)) {
			return false;
		}
		
		if (bigStr == null || "".equals(bigStr)) {
			return false;
		}
		if (bigStr.toUpperCase().indexOf(smallStr.toUpperCase()) == 0) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isStringEmpty(String str) {
		if (str == null || "".equals(str) || str.equalsIgnoreCase("null")) {
			return true;
		}
		return false;
	}

	public static List<Integer> stringToListInt(String source) {
		if (source == null) {
			return null;
		}
		String[] strList = source.split(",");
		List<Integer> intList = new ArrayList<Integer>();
		for (int i = 0; i < strList.length; i++) {
			try {
				intList.add(Integer.parseInt(strList[i]));
			} catch (Exception e) {
			}
		}
		return intList;
	}

	public static String listIntToString(List<Integer> listInt) {
		StringBuilder sb = new StringBuilder();
		if (listInt == null || listInt.size() <= 0) {
			return null;
		}
		for (int i = 0; i < listInt.size(); i++) {
			if (i != 0) {
				sb.append(",");
			}
			sb.append(listInt.get(i));
		}
		return sb.toString();
	}

	public static double[] stringArrayToDoubleArray(String[] args) {
		double[] doublearry = new double[args.length];
		for (int i = 0; i < args.length; i++) {
			doublearry[i] = Double.valueOf(args[i].trim());
		}
		return doublearry;
	}

	public static int[] stringArrayToIntArray(String[] args) {
		int[] intarry = new int[args.length];
		for (int i = 0; i < args.length; i++) {
			intarry[i] = Integer.valueOf(args[i].trim());
		}
		return intarry;
	}
	
	/**
	 * 字符串的压缩
	 * 
	 * @param str
	 *            待压缩的字符串
	 * @return 返回压缩后的字符串
	 * @throws IOException
	 */
	public static byte[] compress(String str) throws IOException {
		if (null == str || str.length() <= 0) {
			return null;
		}
		// 创建一个新的 byte 数组输出流
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		// 使用默认缓冲区大小创建新的输出流
		GZIPOutputStream gzip = new GZIPOutputStream(out);
		// 将 b.length 个字节写入此输出流
		gzip.write(str.getBytes("utf-16le"));
		gzip.close();
		// 使用指定的 charsetName，通过解码字节将缓冲区内容转换为字符串
		return out.toByteArray();
	}

	// GZip解压缩
	public static String uncompress(byte[] str) throws IOException {
		if (str == null || str.length == 0) {
			return "";
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ByteArrayInputStream in = new ByteArrayInputStream(str);
		GZIPInputStream gunzip = new GZIPInputStream(in);
		byte[] buffer = new byte[256];
		int n;
		while ((n = gunzip.read(buffer)) >= 0) {
			out.write(buffer, 0, n);
		}
		// toString()使用平台默认编码，也可以显式的指定如toString(&quot;GBK&quot;)
		return out.toString("utf-16le");
	}

	/**
	 * �?��字符串中是否含有�?�?如果有需要加转义字符�?
	 * @param str
	 * @return
	 */
	public static String formatSqlInfo(String str) {
		String result = "";
		
		if (str == null || str.equals("")) {
			return "";
		}
		
		result = str.replace("'", "''");
		
		return result;
	}

	/**
	 * 转义特殊字符(',",&,<,>)
	 * @param str
	 * @return
	 */
	public static String formatInputContent(String str) {
		String result = "";

		if (str == null || str.equals("")) {
			return "";
		}

		result = str.replace("'", "\'");
		result = str.replace("\"", "\"");
		result = str.replace("&", "\\&");
		result = str.replace("<", "\\<");
		result = str.replace(">", "\\>");
		return result;
	}

	/**
	 * 判断字符串不为null
	 * @param str
	 * @return fasle:null或""  true:非null或非""
	 */
	public static boolean isStringNotEmpty(String str) {
		if (str == null || "".equals(str)) {
			return false;
		}
		return true;
	}

	/**
	 * 将字符串转成MD5值(截取了16位的)
	 *
	 * @param string
	 * @return
	 */
	public static String stringToMD5(String string) {
		String result = null;
		try {
			byte[] hash;

			try {
				hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
				return null;
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				return null;
			}

			StringBuilder hex = new StringBuilder(hash.length * 2);
			for (byte b : hash) {
				if ((b & 0xFF) < 0x10)
					hex.append("0");
				hex.append(Integer.toHexString(b & 0xFF));
			}

			result = hex.toString();
		} catch (Exception e) {
		}

		// 默认可能是32位的
		if (!TextUtils.isEmpty(result)) {
			// 截取16位
			int length = result.length();
			if (length > 16) {
				int start = (length - 16) / 2;
				int end = ((start + 16) < length ? (start + 16) : length);
				result = result.substring(start, end);
			}
		}

		return result;
	}

}
